<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" inline>
                <el-row :gutter="10">
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">编 号</div>
                      <el-input v-model="query.coding" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">名 称</div>
                      <el-input v-model="query.name" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="2" :lg="4">
                    <el-form-item>
                      <div class="search_label">资 产</div>
                      <v-select-s v-model="query.equipment_id" action="eqList" :query="{add_field: 'coding'}" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="2" :lg="3">
                    <el-form-item>
                      <div class="search_label">状 态</div>
                      <el-select v-model="query.pm2status" size="mini">
                        <el-option v-for="(item,statusIndex) in pm2Status" :key="statusIndex" :label="item" :value="statusIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="2" :lg="3">
                    <el-form-item>
                      <div class="search_label">负责人</div>
                      <v-select-s v-model="query.admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">车 间</div>
                      <el-select v-model="query.workshop" size="mini" clearable multiple @change="factoryChange">
                        <el-option v-for="(item,workshopIndex) in planWorkshop" :key="workshopIndex" :label="item" :value="workshopIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">班 组</div>
                      <v-select v-model="query.member" action="classByFactory" :query="{factory: query.workshop}" multiple />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_label">安排时间</div>
                      <el-date-picker v-model="query.arrange" type="dates" placeholder="" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">操作者</div>
                      <v-select-s v-model="query.query_admin_id" action="adminList" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">生产线</div>
                      <v-select-s v-model="query.line_id" action="factoryList" :query="{category: 3}" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div class="search_label">工位</div>
                      <v-select-s v-model="query.process" action="factoryList" multiple :query="{line_id: query.line_id, category: 4}" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <el-form-item>
                      <div class="search_label">操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-box" size="mini" :disabled="!selection.length" @click="archive">停产</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" :disabled="!selection.length" size="mini" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="pmList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" align="center" width="50" />
        <el-table-column type="selection" align="center" width="50" />
        <el-table-column prop="coding" label="编号" align="center" width="150" />
        <el-table-column label="名称" width="250">
          <template scope="scope">
            {{ scope.row.eq.coding }}-{{ scope.row.eq.name }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center" width="" :formatter="statusFormat" />
        <el-table-column prop="sc" label="执行时长" min-width="150" align="center" />
        <el-table-column prop="eq.workshop_name.name" label="车间" width="100" align="center" />
        <el-table-column prop="eq.line_name.name" label="生产线" width="140" align="center" />
        <el-table-column prop="eq.process_name.name" label="工位" width="130" align="center" />
        <el-table-column prop="admin.name" label="负责人" min-width="80" align="center" />
        <el-table-column label="操作者" min-width="100" align="center">
          <template scope="scope">
            <span v-for="m in scope.row.other_member" :key="m.id">{{ m.admin.name }}，</span>
          </template>
        </el-table-column>
        <el-table-column prop="plan" label="周期（天）" min-width="100" align="center">
          <template scope="scope">
            <span v-if="scope.row.plan">{{ scope.row.plan.period }}</span>
            <span v-else>计划已删除</span>
          </template>
        </el-table-column>
        <el-table-column prop="profession" label="专业" min-width="50" align="center" :formatter="professionFormat" />
        <el-table-column prop="priority" label="优先程度" min-width="80" align="center" :formatter="priorityFormat" />
        <el-table-column prop="arrange_date" label="安排时间" min-width="180" align="center" />
        <el-table-column prop="qrcode_time" label="扫码时间" min-width="180" align="center" />
        <el-table-column prop="submit_complate" label="完成时间" min-width="180" align="center" />
        <el-table-column v-if="listHandle" fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/maintain/pm4'
import { pm2Status, applyType, priority, planWorkshop } from '@/utils/options'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getModeFormat', 'archives', 'getPlanProfessionFormat', 'getPlanPriorityFormat'],
  components: {
    VSelect,
    VSelectS
  },
  data() {
    return {
      loading: false,
      pmList: [],
      total: 0,
      query: { page: 1, limit: 15 },
      selection: [],
      pm2Status,
      applyType,
      priority,
      planWorkshop
    }
  },
  computed: {
    tableMaxHeight() {
      return window.innerHeight - 260 + 'px'
    },
    listHandle() {
      return this.$store.state.settings.listHandle
    }
  },
  watch: {},
  created() {
    const query = JSON.parse(sessionStorage.getItem('query'))
    if (query) {
      this.query = query
      sessionStorage.removeItem('query')
    }
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    modeFormat(row) {
      return this.getModeFormat(row)
    },
    index() {
      this.loading = true
      API.pmList(this.query).then(response => {
        this.total = response.data.total
        this.pmList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    archive() {
      const selection = this.selection
      this.archives(API, selection)
    },
    remove() {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      this.delOne(API, id, false)
    },
    updateHandler(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const url = `/maintain/pm4_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      if (row.stop_admin === 0) {
        if (row.archives === 1) {
          return this.$createElement('div', { 'style': { 'color': 'blue' }}, '停产申请')
        } else {
          if (row.bz_check === 0 && row.engineer_check === 0) {
            // 无需审核
            if (row.status === 0) {
              return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待办')
            } else if (row.status === 1) {
              return this.$createElement('div', { 'style': { 'color': 'green' }}, '执行完毕')
            }
          }
          if (row.bz_check === 1 && row.engineer_check === 0) {
            // 仅班长确认
            if (row.status === 0) {
              return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待办')
            } else if (row.status === 1) {
              return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待班长确认')
            } else if (row.status === 2) {
              return this.$createElement('div', { 'style': { 'color': 'green' }}, '执行完毕')
            }
          }
          if (row.bz_check === 0 && row.engineer_check === 1) {
            // 仅工程师确认
            if (row.status === 0) {
              return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待办')
            } else if (row.status === 1) {
              return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待工程师确认')
            } else if (row.status === 3) {
              return this.$createElement('div', { 'style': { 'color': 'green' }}, '执行完毕')
            }
          }
          if (row.bz_check === 1 && row.engineer_check === 1) {
            // 班长和工程师都需要确认
            if (row.status === 0) {
              return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待办')
            } else if (row.status === 1) {
              return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待班长确认')
            } else if (row.status === 2) {
              return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待工程师确认')
            } else if (row.status === 3) {
              return this.$createElement('div', { 'style': { 'color': 'green' }}, '执行完毕')
            }
          }
          if (row.status === -1) {
            return this.$createElement('div', { 'style': { 'color': 'gray' }}, '待派发')
          }
          if (row.status === 10) {
            return this.$createElement('div', { 'style': { 'color': 'red' }}, '超时')
          }
        }
      } else if (row.stop_admin === 1) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '停产')
      }
    },
    priorityFormat(row) {
      return this.getPlanPriorityFormat(row)
    },
    professionFormat(row) {
      return this.getPlanProfessionFormat(row)
    },
    factoryChange(value) {
      if (value !== this.queryWorkshop) {
        this.query.member = null
        this.queryWorkshop = value
      }
    }
  }
}
</script>
